package com.demo.config;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.demo.enums.ResponseStatus;
import com.demo.web.response.Response;

import lombok.extern.slf4j.Slf4j;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 当SpringMVC出现错误的时候跳转的error页面
 * 
 * @version 1.0
 */
@ApiIgnore
@Slf4j
@RestController
public class HttpErrorController implements ErrorController {

    @GetMapping(path = "/error")
    @org.springframework.web.bind.annotation.ResponseStatus(HttpStatus.OK)
    public Response<?> error(HttpServletRequest request, HttpServletResponse response) {
        log.error("error 错误代码：{},{}", response.getStatus(), request.getRequestURI());
        switch (response.getStatus()) {
            case 400:
                return Response.error(ResponseStatus.RESOURCE_NOT_FOUND, "请校验请求参数");
            case 404:
                return Response.error(ResponseStatus.RESOURCE_NOT_FOUND, "资源不存在");
            default:
                return Response.error(ResponseStatus.ERROR, "其他异常,请联系管理员");
        }
    }

}
